/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.*;
import java.util.Vector;
import javax.microedition.lcdui.*;

/**
 *
 * @author Hallucinogen
 */
public class Utilities {

    public Utilities() {
    }

    public static Utilities getInstance() {
        if (instance == null) {
            instance = new Utilities();
        }

        return instance;
    }

    /**
     * Scale the input image to size width and height preserving the size ratio
     * @param img
     * @param width
     * @param height
     * @return
     */
    public Image getScaledImage(Image img, int width, int height) {
        Image res = Image.createImage(width, height);
        Graphics g = res.getGraphics();

        int sourceWidth = img.getWidth();
        int sourceHeight = img.getHeight();
        float scaleFactor = 1;


        if (width <= 0) {
            scaleFactor = height / sourceHeight;
        } else if (height <= 0) {
            scaleFactor = width / sourceWidth;
        } else {
            float sf1 = (float) height / (float) sourceHeight;
            float sf2 = (float) width / (float) sourceWidth;

            scaleFactor = sf1 < sf2 ? sf1 : sf2;
        }

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                g.setClip(x, y, 1, 1);
                int dx = (int) (x / scaleFactor);
                int dy = (int) (y / scaleFactor);

                g.drawImage(img, x - dx, y - dy, Graphics.TOP | Graphics.LEFT);
            }
        }

        return res;
    }

    /**
     * Read file content and return it as string.
     * @param file The file that will be read.
     * @return A string containing the contents of the file.
     */
    public String readFile(String file) {
        //Read file string
        InputStream text = getClass().getResourceAsStream(file);

        //Initialize vairable
        StringBuffer buffer = new StringBuffer();
        int character = 0;

        try {
            //Get character
            character = text.read();

            //While character is valid
            while (character != -1) {
                //Add it to buffer
                buffer.append((char) character);
                character = text.read();
            }

            //Return the buffer
            return buffer.toString();

        } catch (Exception ex) {
        }

        //Return no string
        return null;
    }

    /**
     * @return the surat
     */
    public Vector getSurat() {
        return getVectorContain("SURAT");
    }

    /**
     * @param surat the surat to set
     */
    public void setSurat(Vector surat) {
        this.surat = surat;
    }

    public Vector getVectorContain(String content) {
        Vector vects = new Vector();

        if (isiFile == null) {
            isiFile = Utilities.getInstance().readFile("/res/data/content.txt");
        }


        int startIdx = isiFile.indexOf(content, 0);

        if (startIdx == -1) {
            return null;
        }

        int endIdx = isiFile.indexOf("}", startIdx);

        String vect = "";

        startIdx = isiFile.indexOf("\"", startIdx);

        while (startIdx < endIdx) {
            ++startIdx;

            if (isiFile.charAt(startIdx) == '\"') {
                int bef = startIdx;
                startIdx = isiFile.indexOf("\"", startIdx + 1);

                if (startIdx < bef) {
                    startIdx = endIdx;
                }

                vects.addElement(vect);
                vect = "";
            } else {
                vect += isiFile.charAt(startIdx);
            }

        }
        return vects;

    }
    //String
    private Vector surat;
    private static Utilities instance;
    private static String isiFile;
}
